using System.Collections.Generic;
using GameStateManagement.View.Gui.Components;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace GameStateManagement.View.Gui
{
    /// <summary>
    /// Handles Gui components and should handle gestures.
    /// </summary>
    public class GuiManager
    {
        private SpriteBatch _spriteBatch;

        private readonly List<GuiComponent> _components;

        public GuiManager(SpriteBatch spriteBatch)
        {
            _spriteBatch = spriteBatch;
            _components = new List<GuiComponent>();
        }

        public void AddComponent(GuiComponent component)
        {
            _components.Add(component);
        }

        public bool HandleDragCompleteAsTap { get; set; }

        public bool HandleClick(Vector2 position)
        {
            int i = 0;
            bool clickHandled = false;
            while (!clickHandled && i < _components.Count)
            {
                var component = _components[i];
                clickHandled = component.IsClicked(position);
                ++i;
            }
            return clickHandled;
        }

        public void Update(GameTime gameTime)
        {
            foreach (GuiComponent component in _components)
            {
                component.Update(gameTime);
            }
        }

        public void Draw(GameTime gameTime)
        {
            foreach (GuiComponent component in _components)
            {
                component.Draw(gameTime, _spriteBatch);
            }
        }
    }
}
